package com.lsh.payment.core.util.pay.cm;

import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * @author peter
 */
@Slf4j
public class HttpCmProxy {
    private String api_domain;
    private final static String CHARSET_UTF8 = "utf8";

    public HttpCmProxy(String domain) {
        api_domain = domain;
    }

    /**
     * 计算字符串的MD5值
     *
     * @param signStr:签名字符串
     * @return
     */
    public String getSign(String signStr) {
        try {
            String md5 = MD5CmUtil.encryptMd5(signStr);
            return md5;
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
            return null;
        }
    }


    public String getClient_Sn(int codeLenth) {
        while (true) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < codeLenth; i++) {
                if (i == 0) {
                    sb.append(new Random().nextInt(9) + 1);
                } else {
                    sb.append(new Random().nextInt(10));
                }
            }
            return sb.toString();
        }
    }

    /**
     * 终端激活
     *
     * @param code:激活码
     * @param vendor_sn:服务商序列号
     * @param vendor_key:服务商密钥
     * @param appid:应用编号
     * @return {terminal_sn:"$终端号",terminal_key:"$终端密钥"}
     */
    public String activate(String vendor_sn, String vendor_key, String appid, String code) {
        String url = api_domain + "/terminal/activate";
        JSONObject params = new JSONObject();
        try {
            params.put("appid", appid);
            params.put("code", code);
            params.put("device_id", "LSH6001SERVICE01");
            params.put("client_sn", "LSH60010027");
            params.put("name", "链商收银服务60010027");
            params.put("os_info", "Linux");
            params.put("sdk_version", "Java SDK v1.0");

            String sign = getSign(params.toString() + vendor_key);

            String result = HttpCmUtil.httpPost(url, params.toString(), sign, vendor_sn);
            JSONObject retObj = new JSONObject(result);
            String resCode = retObj.get("result_code").toString();
            if (!resCode.equals("200")) {
                return null;
            }

            return retObj.get("biz_response").toString();
        } catch (Exception e) {

            log.error("终端激活 异常", e);
        }

        return null;
    }

    /**
     * 终端签到
     *
     * @param terminal_sn:终端号
     * @param terminal_key:终端密钥
     * @return {terminal_sn:"$终端号",terminal_key:"$终端密钥"}
     */
    public String checkin(String terminal_sn, String terminal_key) {
        String url = api_domain + "/terminal/checkin";
        JSONObject params = new JSONObject();
        try {
            params.put("terminal_sn", terminal_sn);
            params.put("device_id", "LSH6001SERVICE01");
            params.put("os_info", "Linux");
            params.put("sdk_version", "Java SDK v1.0");
            String sign = getSign(params.toString() + terminal_key);
            String result = HttpCmUtil.httpPost(url, params.toString(), sign, terminal_sn);
            JSONObject retObj = new JSONObject(result);
            String resCode = retObj.get("result_code").toString();
            if (!resCode.equals("200")) {
                return null;
            }

            return retObj.get("biz_response").toString();
        } catch (Exception e) {
            return null;
        }
    }

//    /**
//     * 付款
//     *
//     * @param terminal_sn:终端号
//     * @param terminal_key:终端密钥
//     * @return
//     */
//    public String pay(String terminal_sn, String terminal_key) {
//        String url = api_domain + "/upay/v2/pay";
//        JSONObject params = new JSONObject();
//        try {
//            params.put("terminal_sn", terminal_sn);           //终端号
//            params.put("client_sn", getClient_Sn(16));  //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
//            params.put("total_amount", "1000");               //交易总金额,以分为单位
//            params.put("payway", "1");                         //支付方式,1:支付宝 3:微信 4:百付宝 5:京东钱包
//            params.put("dynamic_id", "130818341921441147");     //条码内容
//            params.put("subject", "Pizza");                     //交易简介
//            params.put("operator", "kay");                     //门店操作员
//
//            String sign = getSign(params.toString() + terminal_key);
//            String result = HttpCmUtil.httpPost(url, params.toString(), sign, terminal_sn);
//            return result;
//        } catch (Exception e) {
//            return null;
//        }
//    }

//    /**
//     * 退款
//     *
//     * @param terminal_sn:终端号
//     * @param terminal_key:终端密钥
//     * @return
//     */
//    public String refund(String terminal_sn, String terminal_key) {
//        String url = api_domain + "/upay/v2/refund";
//        JSONObject params = new JSONObject();
//        try {
//            params.put("terminal_sn", terminal_sn);            //收钱吧终端ID
//            params.put("sn", "7892259488292938");              //收钱吧系统内部唯一订单号
//            params.put("client_sn", "18348290098298292838");   //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
//            params.put("refund_amount", "1000");               //退款金额
//            params.put("refund_request_no", "23030349");          //商户退款所需序列号,表明是第几次退款
//            params.put("operator", "kay");                      //门店操作员
//
//            String sign = getSign(params.toString() + terminal_key);
//            String result = HttpCmUtil.httpPost(url, params.toString(), sign, terminal_sn);
//
//            return result;
//        } catch (Exception e) {
//            return null;
//        }
//    }

    /**
     * 查询
     *
     * @param params:终端密钥
     * @return
     */
    public String query(JSONObject params, String terminal_sn, String terminal_key) {
        String url = api_domain + "/upay/v2/query";
        try {
            return HttpCmUtil.httpPost(url, params.toString(), this.getSign(params.toString() + terminal_key), terminal_sn);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 自动撤单
     *
     * @param terminal_sn:终端号
     * @param terminal_key:终端密钥
     * @return
     */
    public String cancel(String terminal_sn, String terminal_key) {
        String url = api_domain + "/upay/v2/cancel";
        JSONObject params = new JSONObject();
        try {
            params.put("terminal_sn", terminal_sn);           //终端号
            params.put("sn", "7892259488292938");             //收钱吧系统内部唯一订单号
            params.put("client_sn", "18348290098298292838");  //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节

            return HttpCmUtil.httpPost(url, params.toString(), this.getSign(params.toString() + terminal_key), terminal_sn);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 预下单
     *
     * @return
     */
    public String precreate(JSONObject params, String terminal_sn, String terminal_key) {
        String url = api_domain + "/upay/v2/precreate";
        try {
            return HttpCmUtil.httpPost(url, params.toString(), this.getSign(params.toString() + terminal_key), terminal_sn);
        } catch (Exception e) {
            return null;
        }
    }

}
